<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\ProductImg;
use App\Models\Product;
use OSS;

class ProductImgController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = [];
        $product  = [];
        if($request->product_id) {
            $product = Product::find($request->product_id);
            $where['product_id'] = (int)$request->product_id;
        }

        $OSS = env('OSS_URL');
        $list = ProductImg::where($where)->paginate(20);
        return view('admin.product.img',compact('list','product','OSS'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        $data = ProductImg::find($id);
        $img   = $data->imgurl;
        
        $product_id = $data->product_id;
        if($data->thumb) {
            OSS::deleteObject("xmistz", $data->thumb); 
        }        
        $rs = OSS::deleteObject("xmistz", $img); 
        $data->delete();
        return redirect('zadmin/product-imgs?product_id='.$product_id);      
        
    }


    
}
